package gold.contest;

import gold.utils.InputUtil;
import javafx.util.Pair;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC5203 {
    public long startExecuteTime = System.currentTimeMillis();


    /**
     * 这出的都是啥傻逼题目，还暴力法
     * 亏我想半天treemap
     */
    class Solution {
        public int digArtifacts(int n, int[][] artifacts, int[][] dig) {
            //记录单元格是否已经裸露
            boolean[] f = new boolean[n * n];
            for (int[] d : dig) {
                int idx = d[0] * n + d[1];
                f[idx] = true;
            }

            int ans = 0;
            for (int[] art : artifacts) {
                // 是否裸露
                boolean flag = true;
                for (int r = art[0]; r <= art[2] && flag; r++) {
                    for (int c = art[1]; c <= art[3] && flag; c++) {
                        int idx = r * n + c;
                        flag = f[idx];
                    }
                }

                // 范围内所有单元格都已经裸露
                if (flag) ans++;
            }

            return ans;
        }
    }


    class Solution_Slow_Thinking {
        public int digArtifacts(int n, int[][] artifacts, int[][] dig) {
            TreeMap<Integer, TreeMap<Integer, Pair<int[], Integer>>> artifactMap = new TreeMap<>();

            for (int[] artifact : artifacts) {
                int row1 = artifact[0], row2 = artifact[2], col1 = artifact[1], col2 = artifact[3];
                int size = (row2 - row1 + 1) * (col2 - col1 + 1);
                if (!artifactMap.containsKey(row1)) artifactMap.put(row1, new TreeMap<>());
                if (!artifactMap.get(row1).containsKey(col1))
                    artifactMap.get(row1).put(col1, new Pair<>(artifact, size));
            }

            int ans = 0;
            for (int[] di : dig) {
                Integer nearRow = artifactMap.floorKey(di[0]);
                if (null == nearRow) continue;
                Integer nearCol = artifactMap.get(nearRow).floorKey(di[1]);
                if (null == nearCol) continue;
                Pair<int[], Integer> nearBlock = artifactMap.get(nearRow).get(nearCol);

            }

            return ans;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC5203 an = new LC5203();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
